/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_connect_unpack.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/21 by geyh                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "../../../../src/module/oracle/config.h"
#include <oracle/ora_core.h>
#include <oracle/comm/ora_comm.h>
#include <oracle/tns/ora_connect.h>
#include <frct_comm.h>
#include "../../test_ora_comm.h"

#include "t_ora_connect.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}


static void __case1(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L7_ora_packet *p_st_ora_packet = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    p_st_ora_packet = p_st_session_private->p_st_ora_packet;

    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x00\xa7\x00\x00\x01\x00\x00\x00\x01\x34\x01\x2c\x00\x00\x08\x00"
            "\x7f\xff\x4f\x98\x00\x00\x00\x01\x00\x85\x00\x22\x00\x00\x00\x00\x01\x01\x28\x44\x45\x53\x43\x52\x49\x50\x54"
            "\x49\x4f\x4e\x3d\x28\x43\x4f\x4e\x4e\x45\x43\x54\x5f\x44\x41\x54\x41\x3d\x28\x53\x49\x44\x3d\x6f\x72\x63\x6c"
            "\x29\x28\x43\x49\x44\x3d\x28\x50\x52\x4f\x47\x52\x41\x4d\x3d\x29\x28\x48\x4f\x53\x54\x3d\x5f\x5f\x6a\x64\x62"
            "\x63\x5f\x5f\x29\x28\x55\x53\x45\x52\x3d\x29\x29\x29\x28\x41\x44\x44\x52\x45\x53\x53\x3d\x28\x50\x52\x4f\x54"
            "\x4f\x43\x4f\x4c\x3d\x74\x63\x70\x29\x28\x48\x4f\x53\x54\x3d\x31\x39\x32\x2e\x31\x36\x38\x2e\x31\x38\x2e\x32"
            "\x33\x34\x29\x28\x50\x4f\x52\x54\x3d\x31\x35\x32\x31\x29\x29\x29", 167);

    i_ret = ora_connect_unpack(p_st_conn, p_st_sess, p_st_ora_packet);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_ora_packet->p_st_packet);

    test_ora_destroy_ct(&p_st_conn);

    return;

}


static void __case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L7_ora_packet *p_st_ora_packet = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    p_st_ora_packet = p_st_session_private->p_st_ora_packet;

    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x01\x02\x00\x00\x01\x00\x00\x00\x01\x39\x01\x2c\x00\x00\x08\x00"
            "\x7f\xff\xc6\x0e\x00\x00\x01\x00\x00\xc8\x00\x3a\x00\x00\x08\x00\x41\x41\x00\x00\x00\x00\x00\x00\x00\x00\x00"
            "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x44\x45\x53\x43\x52\x49\x50\x54\x49\x4f\x4e"
            "\x3d\x28\x43\x4f\x4e\x4e\x45\x43\x54\x5f\x44\x41\x54\x41\x3d\x28\x53\x45\x52\x56\x49\x43\x45\x5f\x4e\x41\x4d"
            "\x45\x3d\x6f\x72\x63\x6c\x29\x28\x43\x49\x44\x3d\x28\x50\x52\x4f\x47\x52\x41\x4d\x3d\x44\x3a\x5c\x73\x6f\x66"
            "\x74\x77\x61\x72\x65\x5c\x4e\x61\x76\x69\x63\x61\x74\x3f\x66\x6f\x72\x3f\x4f\x72\x61\x63\x6c\x65\x5c\x69\x6e"
            "\x73\x74\x61\x6e\x74\x63\x6c\x69\x65\x6e\x74\x5f\x31\x30\x5f\x32\x5c\x73\x71\x6c\x70\x6c\x75\x73\x2e\x65\x78"
            "\x65\x29\x28\x48\x4f\x53\x54\x3d\x4e\x4f\x4e\x4b\x52\x29\x28\x55\x53\x45\x52\x3d\x57\x69\x6e\x29\x29\x29\x28"
            "\x41\x44\x44\x52\x45\x53\x53\x3d\x28\x50\x52\x4f\x54\x4f\x43\x4f\x4c\x3d\x54\x43\x50\x29\x28\x48\x4f\x53\x54"
            "\x3d\x31\x37\x32\x2e\x31\x36\x2e\x30\x2e\x37\x35\x29\x28\x50\x4f\x52\x54\x3d\x31\x35\x32\x31\x29\x29\x29", 258);

    i_ret = ora_connect_unpack(p_st_conn, p_st_sess, p_st_ora_packet);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    bytebuff_free(&p_st_ora_packet->p_st_packet);

    test_ora_destroy_ct(&p_st_conn);

    return;

}

static void __case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L7_ora_packet *p_st_ora_packet = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    p_st_ora_packet = p_st_session_private->p_st_ora_packet;

    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x00\xa7\x00\x00\x01\x00\x00\x00\x01\x34\x01\x2c\x00\x00\x08\x00"
            "\x7f\xff\x4f\x98\x00\x00\x00\x01\x00\x85\x00\x22\x00\x00\x00\x00\x01\x01\x28\x44\x45\x53\x43\x52\x49\x50\x54"
            "\x49\x4f\x4e\x3d\x28\x43\x4f\x4e\x4e\x45\x43\x54\x5f\x44\x41\x54\x41\x3d\x28\x53\x49\x44\x3d\x6f\x72\x63\x6c"
            "\x29\x28\x43\x49\x44\x3d\x28\x50\x52\x4f\x47\x52\x41\x4d\x3d\x29\x28\x48\x4f\x53\x54\x3d\x5f\x5f\x6a\x64\x62"
            "\x63\x5f\x5f\x29\x28\x55\x53\x45\x52\x3d\x29\x29\x29\x28\x41\x44\x44\x52\x45\x53\x53\x3d\x28\x50\x52\x4f\x54"
            "\x4f\x43\x4f\x4c\x3d\x74\x63\x70\x29\x28\x48\x4f\x53\x54", 138);

    i_ret = ora_connect_unpack(p_st_conn, p_st_sess, p_st_ora_packet);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_ora_packet->p_st_packet);

    test_ora_destroy_ct(&p_st_conn);

    return;

}


static void __case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_conn = NULL;
    L7_ora_packet *p_st_ora_packet = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    L7session_ora *p_st_sess = p_st_session_private->p_st_sess;

    p_st_ora_packet = p_st_session_private->p_st_ora_packet;

    bytebuff_add(p_st_ora_packet->p_st_packet, (char *)"\x01\x02\x00\x00\x01\x00\x00\x00\x01\x39\x01\x2c\x00\x00\x08\x00"
            "\x7f\xff\xc6\x0e\x00\x00\x01\x00\x00\xc8\x00\x3a\x00\x00\x08\x00\x41\x41\x00\x00\x00\x00\x00\x00\x00\x00\x00"
            "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x44\x45\x53\x43\x52\x49\x50\x54\x49\x4f\x4e"
            "\x3d\x28\x43\x4f\x4e\x4e\x45\x43\x54\x5f\x44\x41\x54\x41\x3d\x28\x53\x45\x52\x56\x49\x43\x45\x5f\x4e\x41\x4d"
            "\x45\x3d\x6f\x72\x63\x6c\x29\x28\x43\x49\x44\x3d\x28\x50\x52\x4f\x47\x52\x41\x4d\x3d\x44\x3a\x5c\x73\x6f\x66"
            "\x74\x77\x61\x72\x65\x5c\x4e\x61\x76\x69\x63\x61\x74\x3f\x66\x6f\x72\x3f\x4f\x72\x61\x63\x6c\x65\x5c\x69\x6e"
            "\x73\x74\x61\x6e\x74\x63\x6c\x69\x65\x6e\x74\x5f\x31\x30\x5f\x32\x5c\x73\x71\x6c\x70\x6c\x75\x73\x2e\x65\x78"
            "\x65\x29\x28\x48\x4f\x53\x54\x3d\x4e\x4f\x4e\x4b\x52\x29\x28\x55\x53\x45\x52\x3d\x57\x69\x6e\x29\x29\x29\x28"
            "\x41\x44\x44\x52\x45\x53\x53\x3d\x28\x50\x52\x4f\x54\x4f\x43\x4f", 221);

    i_ret = ora_connect_unpack(p_st_conn, p_st_sess, p_st_ora_packet);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_ora_packet->p_st_packet);

    test_ora_destroy_ct(&p_st_conn);

    return;

}

int add_suite_ora_connect_unpack(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_connect_unpack()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
